package org.budo.activiti.execution;

import java.util.Map;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.budo.activiti.service.api.BudoActivitiService;
import org.budo.activiti.util.VariableScopeUtil;
import org.budo.support.spring.util.SpringUtil;

/**
 * @author lmw
 */
public class ExecutionListenerDispatcher implements ExecutionListener {
    private static final long serialVersionUID = 7445102431313695372L;

    @Override
    public void notify(DelegateExecution execution) {
        String listenerType = this.getClass().getSimpleName();

        Map<String, Object> variable = VariableScopeUtil.getVariable(execution);
        variable.put("listenerType", listenerType);

        String modelId = this.budoActivitiService().findModelIdByprocessInstanceId(execution.getProcessInstanceId());
        variable.put("modelId", modelId);

        this.executionListenerDelegate().onExecution(variable);
    }

    private BudoActivitiService budoActivitiService() {
        return SpringUtil.getBean(BudoActivitiService.class);
    }

    private ExecutionListenerDelegate executionListenerDelegate() {
        return SpringUtil.getBean(ExecutionListenerDelegate.class);
    }
}